package pl.edu.mimuw.irs.rcp.controller.editor.roles.handler;

import java.util.Set;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.edu.mimuw.irs.core.model.design.net.IRSWorkflow;
import pl.edu.mimuw.irs.core.model.design.resource.Role;
import pl.edu.mimuw.irs.rcp.controller.editor.roles.command.DeleteWorkflowRoleCommand;
import pl.edu.mimuw.irs.rcp.controller.editor.util.command.DeleteMemberCommand;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.handler.AbstractCommandStackSelectionHandler;
import pl.edu.mimuw.irs.rcp.persistence.PersistenceManager;
import pl.edu.mimuw.irs.rcp.util.Logger;
import pl.edu.mimuw.irs.rcp.util.MessageCode;
import pl.edu.mimuw.irs.rcp.util.UITexts;

public class DeleteRoleHandler extends AbstractCommandStackSelectionHandler {

	// *** Attributes
	
	// *** Methods

	@Override
	protected Object execute(ExecutionEvent event, CommandStack stack,
			Object[] selection) throws ExecutionException {
		
		Shell shell = HandlerUtil.getActiveShell(event);
		/* We must check if workflow does not use this role */
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
		if (editor == null) return null;
		IEditorInput input = editor.getEditorInput();
		if (input == null) return null;
		IRSWorkflow workflow;
		try {
			PersistenceManager manager = PersistenceManager.getInstance(input);
			if (manager == null) throw new Exception(input.toString());
			workflow = manager.getWorkflow();
			if (workflow == null) throw new Exception(input.toString());
		} catch (Exception e) {
			Logger.handle(MessageCode.PERSISTENCE_MANAGER_PROBLEM, e, input.getName());
			return false;
		}
		
		Set<Role> usedRoles = workflow.getUsedRoles();
		
		for (Object object : selection) {
			if (object instanceof Role) {
				Role role = (Role) object;
				Command command;
				
				if (usedRoles.contains(role)) {
					if (!prompt(shell, role)) continue;
					CompoundCommand compound = new CompoundCommand();
					compound.add(new DeleteWorkflowRoleCommand(workflow, role));
					compound.add(new DeleteMemberCommand(role));
					command = compound;
				} else {
					command = new DeleteMemberCommand(role);
				}
				stack.execute(command);
			}
		}
		
		return null;
	}

	protected boolean prompt(Shell shell, Role role) {
		MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
		dialog.setText(UITexts.get(MessageCode.QUESTION_TITLE));
		dialog.setMessage(UITexts.get(MessageCode.ROLE_DELETE_QUESTION) + "\n" + role.getName());
		return (dialog.open() & SWT.OK) == SWT.OK;
	}
}
